;
(function (call) {
    var main = {
        userInfo: null,
        mapId: "jcybMap",
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            this.queryEventAll();
        },
        // 查询事件
        queryEventAll: function () {
            var $select = $("#processEventType").empty();
            $.getJSON(api.internal.business.queryEventAll, function (res) {
                if (!res.isSuccess || !res.data) return;
                var data = res.data;
                for (var i = 0; i < data.length; i++) {
                    var startTime = new Date(data[i].startTime).format('yyyy-MM-dd hh:mm');
                    var endTime = new Date(data[i].endTime).format('yyyy-MM-dd hh:mm');
                    $select.append(`<option data-id="${data[i].id}"  value="${startTime + ',' + endTime}">${data[i].name}</option>`);
                }
                $select.change();
            })
        },
        // 流程监控数据
        getProcessMonitorData: function () {
            var that = this;
            var startTime = $(".processStartTime").text() + ":00";
            var endTime = $(".processEndTime").text() + ":00";
            var areaCode = $("#processUnit").find("option:selected").text();
            var event = $("#processEventType option:selected").data('id')
            // 时间列表
            var timeLine = that.getProcessHours(startTime, endTime), timeList = [];
            var $time = $(".timelist").empty();
            var $timeList = $(".content-right-main");
            var w = (timeLine.length) * 108;
            $time.css('width', '' + w + 'px');
            $timeList.css('width', '' + w + 'px');
            for (var i = 0; i < timeLine.length; i++) {
                timeList.push(`<li title="${new Date(timeLine[i]).format('yyyy-MM-dd hh:mm')}">${new Date(timeLine[i]).format('MM-dd hh:mm')}</li>`);
            }
            $time.append(timeList);

            var params = {
                event: event
            }
            var startHours = new Date(startTime).format('yyyy-MM-dd hh:00:00')
            startHours = new Date(startHours).getTime();
            var $contentRight = $(".content-right-main").empty()
            loading.add('#modal_monitor');
            $.getJSON(api.internal.business.queryFlowMontiorByEvent, params, function (res) {
                loading.remove();
                if (!res.isSuccess) return;
                if (!res.data) {
                    alert('无数据')
                    return;
                };
                var data = res.data, dataArr = [arr1 = [], arr2 = [], arr3 = [], arr4 = []];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].type == 1) {
                        arr1.push(data[i])
                    } else if (data[i].type == 2) {
                        arr2.push(data[i])
                    } else if (data[i].type == 3) {
                        arr3.push(data[i])
                    } else {
                        arr4.push(data[i])
                    }
                }
                var k = 1;
                for (var i = 0; i < dataArr.length; i++) {
                    var li = '<li>'
                    for (var j = 0; j < dataArr[i].length; j++) {
                        var item = dataArr[i][j];
                        var publishTime = new Date(item.publishTime).format('yyyy-MM-dd hh:00:00');
                        var oneHours = (new Date(publishTime).getTime() - startHours) / (60 * 60 * 1000);
                        var left = oneHours * 108;
                        if (j > 0) {
                            var prevItem = dataArr[i][j - 1]
                            var prevPublishTime = new Date(prevItem.publishTime).format('yyyy-MM-dd hh:00:00')
                            if (publishTime == prevPublishTime) {
                                left = (oneHours * 108) + 15 * k++;
                            } else {
                                k = 1;
                            }
                        }
                        li += '<span data-id="' + item.id + '" style="left:' + left + 'px"  title = "' + item.publishContent + '" ><i>×</i>' + item.monitorDesc + '</span > '
                    }
                    li += '</li>'
                    var $li = $(li);
                    $contentRight.append($li);
                }
            })
        },
        deleteFlowMonitor: function (item) {
            var id = $(item).parent().data('id');
            var param = { "id": id };
            $.ajax({
                url: api.internal.business.deleteFlowMonitor,
                type: "get",
                dataType: 'json',
                data: param,
                success: function (res) {
                    if (res.isSuccess) {
                        alert("删除成功");
                        $(item).parent().remove();
                    } else {
                        alert("删除失败");
                    }
                }
            })
        },
        // 获取开始结束区间内每个小时
        getProcessTime: function (time) {
            let ymd = '';
            let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1));
            let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate());
            let hours = time.getHours() >= 10 ? time.getHours() : ('0' + time.getHours());
            ymd += time.getFullYear() + '-';
            ymd += mouth + '-';
            ymd += day;
            ymd += ' ' + hours + ':00';
            return ymd;
        },
        getProcessHours: function (start, end) {
            var dateArr = [];
            let ubixDb = new Date(start).getTime();
            let ubixDe = new Date(end).getTime();
            let stamp;
            const oneHours = 60 * 60 * 1000;
            for (stamp = ubixDb; stamp <= ubixDe;) {
                dateArr.push(this.getProcessTime(new Date(parseInt(stamp))));
                stamp = stamp + oneHours;
            }
            return dateArr;
        },
        // 流程监控弹窗
        initprocessPop: function () {
            $("#modal_add_process").remove();
            CafeJS.ModalBox({
                id: 'add_process',
                title: "新增流程",
                width: 562,
                height: 200,
                m_top: 320,
                m_right: 370,
                hide: false,
                isBackdrop: true,
                url: api.internal.tpl.addProcessPop,
                createSucc: function () {
                }
            });
        },
        downloadProcess: function () {
            var areaCode = $("#processUnit").val();
            var event = $("#processEventType option:selected").data('id')
            loading.add('#modal_monitor');
            var params = {
                event: event,
                areaCode: areaCode
            }
            $.getJSON(api.internal.business.getWordPath, params, function (res) {
                if (res.isSuccess) {
                    var data = res.data;
                    loading.remove();
                    var url = api.internal.business.downLoadProcess + '?path=' + encodeURI(data);
                    window.location.href = url;
                }
            })
        },
        bindEvt: function () {
            var that = this;
            $("#processQueryBtn").click(function () {
                that.getProcessMonitorData();
            })
            $("#processAddBtn").click(function () {
                that.initprocessPop();
            })
            $("#processEventType").change(function () {
                var val = $(this).val().split(',');
                $(".processStartTime").text(val[0]);
                $(".processEndTime").text(val[1]);
            })
            $(".content-right-main").on('click', 'li span i', function () {
                that.deleteFlowMonitor($(this))
            })
            $("#processDownloadBtn").click(function () {
                that.downloadProcess();
            })
        },
    };
    if (typeof call === 'function') {
        window.processMonitor = main;
        call(main);
    }
})(function (main) {
    CafeJS.models(['datepicker'], function () {
        main.init();
    });
});